package cate.game.pvp.throne.data;

import cate.common.table.activity.throne.TBTimeRow;
import cate.common.table.activity.throne.TBTimeSheet;
import cate.common.table.d.GDGuildWar;
import cate.common.table.d.GDThrone;
import cate.common.util.TimeTool;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@NoteClass("王座争霸时间表")
@Slf4j
public class TBTimeTable {

	@NoteField("当前的阶段")
	public byte period;

	@NoteField("时间<阶段，结束时间>")
	public Map<Byte, Long> timeMap;

	@NoteField(value = "下次检测时间",detail = "就是当前阶段结束的时间")
	public long checkTime;

	public void initialize() {
		if (timeMap == null) {
			timeMap = new HashMap<>();
		}
	}

	public void buildTimeTable(TBTimeSheet sheet, long startTime) {
		checkTime = 0;
		timeMap.clear();
		for (int i = 1; i <= 5; i++) {
			TBTimeRow row = sheet.get(i);
			if (row != null) {
				startTime += row.lastTime;
			}
			timeMap.put((byte) i, startTime);
		}
		period = GDThrone.Period.PREPARE;
		checkTime = timeMap.get(period);
		printTime();
	}


	@JsonIgnore
	public boolean tick() {
		if (checkTime == 0 || System.currentTimeMillis() < checkTime) {
			return false;
		}
		period = GDThrone.Period.getNextPeriod(period);
		if (period == GDGuildWar.Period.NULL) {
			checkTime = 0;
		} else {
			checkTime = timeMap.get(period) == null ? 0 : timeMap.get(period);
		}
		printTime();
		return true;
	}

	private void printTime(){
		log.info("[王座争霸]-阶段发生变更 目前是[{}] 下次变更时间是:{}", GDThrone.Period.des(period), TimeTool.getDF2().format(new Date(checkTime)));
	}

	public boolean inTrial() {
		return period == GDThrone.Period.TRIAL;
	}

	public boolean inKo(){
		return period == GDThrone.Period.KO;
	}

	public boolean inChampionBoss(){
		return period == GDThrone.Period.CHAMPION_BOSS;
	}

	public boolean inZoneBoss() {
		return period == GDThrone.Period.ZONE_BOSS;
	}
}
